/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pkg_customerManagement;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author mattcarr
 */
public class RegisterCustomer extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");

        String success = "register.jsp?success=1";
        String error = "register.jsp?error=1";

        String name = request.getParameter("na");
        String surname = request.getParameter("surna");
        String email = request.getParameter("email");
        String pass = request.getParameter("pass");
        String passConfirm = request.getParameter("passConf");

        if (validateName(name) != 0) {
            response.sendRedirect(response.encodeRedirectURL(error));
            return;
        }

        if (validateName(surname) != 0) {
            response.sendRedirect(response.encodeRedirectURL(error));
            return;
        }

        if (validateEmail(email) != 0) {
            response.sendRedirect(response.encodeRedirectURL(error));
            return;
        }

        if (validatePassword(pass, passConfirm) != 0) {
            response.sendRedirect(response.encodeRedirectURL(error));
            return;
        }

        CustomerDatabaseFunctions cDF = new CustomerDatabaseFunctions();
        int status = cDF.registerCustomer(email, name, surname, pass);

        if (status == 0) {
            response.sendRedirect(response.encodeRedirectURL(success));
            return;
        } else {
            response.sendRedirect(response.encodeRedirectURL(error));
            return;
        }
    }

    private int validatePassword(String password, String passwordConfirmation) {
        if (password.length() >= 8 && password.equals(passwordConfirmation)) {
            return 0;
        } else {
            return -1;
        }
    }

    private int validateName(String name) {
        if (name.length() > 2) {
            return 0;
        } else {
            return -1;
        }

    }

    private int validateEmail(String email) {
        Pattern p = Pattern.compile("^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");
        //Match the given string with the pattern
        Matcher m = p.matcher(email);
        //check whether match is found
        boolean matchFound = m.matches();
        if (matchFound) {
            return 0;
        } else {
            return -1;
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
